package com.ruoyi.tzai.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.tzai.domain.UserPlayHistory;
import com.ruoyi.tzai.service.IUserPlayHistoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 用户播放历史Controller
 * 
 * @author tzai
 * @date 2025-10-27
 */
@Api(tags = "用户播放历史管理")
@RestController
@RequestMapping("/tzai/history")
public class UserPlayHistoryController extends BaseController
{
    @Autowired
    private IUserPlayHistoryService userPlayHistoryService;

    /**
     * 查询用户播放历史列表
     */
    @ApiOperation("查询用户播放历史列表")
    @PreAuthorize("@ss.hasPermi('tzai:history:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserPlayHistory userPlayHistory)
    {
        startPage();
        List<UserPlayHistory> list = userPlayHistoryService.selectUserPlayHistoryList(userPlayHistory);
        return getDataTable(list);
    }

    /**
     * 导出用户播放历史列表
     */
    @ApiOperation("导出用户播放历史列表")
    @PreAuthorize("@ss.hasPermi('tzai:history:export')")
    @Log(title = "用户播放历史", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserPlayHistory userPlayHistory)
    {
        List<UserPlayHistory> list = userPlayHistoryService.selectUserPlayHistoryList(userPlayHistory);
        ExcelUtil<UserPlayHistory> util = new ExcelUtil<UserPlayHistory>(UserPlayHistory.class);
        util.exportExcel(response, list, "用户播放历史数据");
    }

    /**
     * 获取用户播放历史详细信息
     */
    @ApiOperation("获取用户播放历史详细信息")
    @PreAuthorize("@ss.hasPermi('tzai:history:query')")
    @GetMapping(value = "/{historyId}")
    public AjaxResult getInfo(@PathVariable("historyId") Long historyId)
    {
        return success(userPlayHistoryService.selectUserPlayHistoryByHistoryId(historyId));
    }

    /**
     * 获取用户某篇文章的播放历史
     */
    @ApiOperation("获取用户某篇文章的播放历史")
    @GetMapping(value = "/article")
    public AjaxResult getByArticle(@RequestParam Long userId, @RequestParam Long articleId)
    {
        UserPlayHistory history = userPlayHistoryService.selectUserPlayHistoryByUserIdAndArticleId(userId, articleId);
        return success(history);
    }

    /**
     * 新增用户播放历史
     */
    @ApiOperation("新增用户播放历史")
    @PreAuthorize("@ss.hasPermi('tzai:history:add')")
    @Log(title = "用户播放历史", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserPlayHistory userPlayHistory)
    {
        return toAjax(userPlayHistoryService.insertUserPlayHistory(userPlayHistory));
    }

    /**
     * 修改用户播放历史
     */
    @ApiOperation("修改用户播放历史")
    @PreAuthorize("@ss.hasPermi('tzai:history:edit')")
    @Log(title = "用户播放历史", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserPlayHistory userPlayHistory)
    {
        return toAjax(userPlayHistoryService.updateUserPlayHistory(userPlayHistory));
    }

    /**
     * 删除用户播放历史
     */
    @ApiOperation("删除用户播放历史")
    @PreAuthorize("@ss.hasPermi('tzai:history:remove')")
    @Log(title = "用户播放历史", businessType = BusinessType.DELETE)
	@DeleteMapping("/{historyIds}")
    public AjaxResult remove(@PathVariable Long[] historyIds)
    {
        return toAjax(userPlayHistoryService.deleteUserPlayHistoryByHistoryIds(historyIds));
    }

    /**
     * 更新播放进度
     */
    @ApiOperation("更新播放进度")
    @Log(title = "用户播放历史", businessType = BusinessType.UPDATE)
    @PostMapping("/updateProgress")
    public AjaxResult updateProgress(@RequestParam Long userId, 
                                      @RequestParam Long articleId,
                                      @RequestParam String language,
                                      @RequestParam Integer playProgress,
                                      @RequestParam Integer duration)
    {
        int result = userPlayHistoryService.updatePlayProgress(userId, articleId, language, playProgress, duration);
        return toAjax(result);
    }
}

